import requests
from bs4 import BeautifulSoup


def getHtml(url):
    try:
        re = requests.get(url, timeout=10)
        re.raise_for_status()
        re.encoding = 'utf-8'
        print('获取成功')
        return re.text
    except:
        print('获取失败')
        return ''


def getHtmlData(html):
    soup = BeautifulSoup(html, 'html.parser')
    tas = soup.find_all('div', class_='right-1')
    all_ls = []
    for a in tas[0].find_all('a'):
        ls = []
        for em in a.find_all('em'):
            ls.append(em.string)
        for i in a.find_all('i'):
            ls.append(i.string + '日')
        for h3 in a.find_all('h3'):
            ls.append(h3.string)
        all_ls.append(ls)
    return all_ls


def wroitedata(ls):
    with open(r'学院新闻.csv', 'w') as f:
        for line in ls:
            f.write(','.join(line[0:4]) + '\n')


def main():
    url = 'http://www.zjitc.net/xwzx/xyxw.htm'
    html = getHtml(url)
    getHtmlData(html)
    wroitedata(getHtmlData(html))


main()
